package com.example.nacosprovider;

import com.alibaba.fastjson.JSON;
import com.example.nacosprovider.dao.UserMapper;
import com.example.nacosprovider.pojo.JavaUser;
import com.example.nacosprovider.pojo.UserInfo;
import com.example.nacosprovider.pojo.user;
import com.example.nacosprovider.service.JavaUserService;
import com.example.nacosprovider.util.JsonResult;
import com.example.nacosprovider.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class UserContronller {
    private final JavaUserService javaUserService;
    private final UserMapper userService;
    private final RedisUtils redisUtils;

    @Autowired
    public UserContronller(JavaUserService userService, UserMapper userService1, RedisUtils redisUtils) {
        this.javaUserService = userService;
        this.userService = userService1;
        this.redisUtils = redisUtils;
    }

    /**
     * 登录页面
     *
     * @return
     */
    @GetMapping(value = "/login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    /**
     * 注册页面
     *
     * @return
     */
    @GetMapping(value = "/zhuce")
    public ModelAndView zhuce() {
        return new ModelAndView("zhuce");
    }

    /**
     * 注册函数
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/zhuce", method = {RequestMethod.POST})
    public String zhuce(@RequestBody JavaUser user) {
        //调用mybaties plus方法存储用户
        javaUserService.save(user);
        return JSON.toJSONString(new JsonResult<>("注册成功"));
    }

    /**
     * 登录函数
     */
    //@ResponseBody
    //@RequestMapping(value = "/user/login", method = RequestMethod.POST)
    //public void getUserByName(@RequestBody UserInfo userInfo, HttpServletRequest request) {
    //    System.out.println("执行了login方法");
        //return new JsonResult("ok");
        //获取用户列表
        //List<user> userList = userService.getUserAll();
        //for (user user : userList) {
        //    //用户名密码验证
        //    if (user.getName().equals(userInfo.getUser_name()) && user.getPasswd().equals(userInfo.getPasswd())) {
        //        //Session在此语句被创建并且被监听者监听到
        //        //在session域中存储登录用户信息
        //        request.getSession().setAttribute("loginUser", userInfo.getUser_name());
        //        //当用户登陆时，使用mybatiesPlus修改用户最后一次登陆时间
        //        javaUserService.updateLoginTime(userInfo.getUser_name());
        //
        //        //从Application域中获取在线用户集合
        //        Map loginList = (Map) request.getSession().getServletContext().getAttribute("loginList");
        //        //输出当前在线用户数
        //        //System.out.println("当前登录用户数:"+loginList.size());
        //        redisUtils.getUserList();
        //        return (new JsonResult<>("true"));
        //    }
        //}
        //return (new JsonResult<>("false"));
    //}

    /**
     * 用户管理界面
     * @param request
     * @return 用户列表集合
     */
    @ResponseBody
    @GetMapping("/loginManage")
    private Map loginList(Map map, HttpServletRequest request) {
        //从Application域中获取当前在线用户集合
        List<JavaUser> users = javaUserService.mannageUser(redisUtils.getUserList());
        map.put("loginList",new JsonResult<>(users));
        System.out.println(JSON.toJSONString(new JsonResult<>(users)));
        return map;
    }

    /**
     * 用户注销函数
     * @param map
     * @param request
     * @return 响应数据
     */
    @ResponseBody
    @PostMapping("/loginout")
    public String loginOut(@RequestBody Map<String,String> map ,HttpServletRequest request) {
        //从Application域中获取当前在线用户集合
        //Map loginList = (Map) request.getSession().getServletContext().getAttribute("loginList");
        String userName = map.get("username");
        System.out.println(userName);
        System.out.println(redisUtils.delSession(userName));
        //根据用户名从用户集合中获取该用户的session
        //HttpSession session= request.getSession();
        //销毁用户Session
        //session.invalidate();
        //String sessionID = request.getSession().getId();
        //System.out.println(sessionID);
        //System.out.println(redisUtils.delSession(sessionID));
        return JSON.toJSONString(new JsonResult<>("ok"));
    }

    /**
     * 头像上传函数
     * @param userName 用户名
     * @param headshotPath 头像路径
     */
    @ResponseBody
    @PostMapping("/updateHeadshotPath")
    public String updateHeadshotPath(@RequestParam(value = "userName") String userName, @RequestBody String headshotPath){
        javaUserService.upLoadHeadsHot(userName,headshotPath);
        return "头像上传成功";
    }
    //@ResponseBody
    //@PostMapping("/save_tiezi")
    //public void save_tiezi(String userName, String tieziContent){
    //    userService.
    //}


}
